package com.zombie.logic.object;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.zombie.logic.Formulas;
import com.zombie.logic.core.ZombieWorld;
import com.zombie.logic.enums.ObjectType;
import com.zombie.logic.item.Weapon;
import com.zombie.util.Utils;


public class Bullet extends GameObject {

	public int damage = 10;
	public float density = 0.1f;
	public float damping = 0.1f;
	public long lifeTime = 5000;
	static ShapeRenderer renderer;
	public boolean strongBullet = false;
	public GameObject lastIntersected;
	
	public Bullet(float f, float g, LiveObject own, float angle) {
		this(f,g,own,angle,4,4);
	}

	public Bullet(float f, float g, LiveObject own, float angle,float width,float height) {
		super(f, g);
		type = ObjectType.BULLET;
		owner = own;
		this.angle = angle;
		size = new Vector2(width,height);
		if (renderer == null)
			renderer = new ShapeRenderer();
		if (Utils.rnd.nextInt(3)  == 0){
//			TracerEffect tracer = new TracerEffect();
//			tracer.setShape(new Line(pos.x,pos.y, pos.x,pos.y));
//			tracer.setFullLifeTime(600L+ Utils.rnd.nextInt(300));
//			tracer.setOwner(this);
//			GameState.addEffect(tracer);
		}

	}	
	
	public Bullet(float x, float y, LiveObject object, float angle, boolean b,Color start, Color finish) {
		super(x, y);
		type = ObjectType.BULLET;
		owner = object;
		this.angle = angle;
		size = new Vector2(4,4);
//		if (b){
//			TracerEffect tracer = new TracerEffect();
//			tracer.start = start;
//			tracer.finish = finish;
//			tracer.setShape(new Line(x,y, x,y));
//			tracer.setFullLifeTime(300L+ Utils.rnd.nextInt(300));
//			tracer.setOwner(this);
//			GameState.addEffect(tracer);
//		}
		createBody();
	}

	@Override
	public void update(float delta) {
		super.update(delta);
		lifeTime-=delta*1000;
		if (lifeTime <= 0)
			remove();
		if (body == null)
			return;
		float x1 = getVelocity()*delta*MathUtils.cos(angle*MathUtils.degRad);
		float y1 = getVelocity()*delta*MathUtils.sin(angle*MathUtils.degRad);
		body.applyForce(new Vector2((float)x1,(float)y1), body.getWorldCenter());
	}

	@Override
	public void draw(SpriteBatch batch, ShapeRenderer shapeBatch) {
		if (damage == 0)
			return;
//		batch.end();
		Gdx.gl.glEnable(GL10.GL_BLEND);
	    Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		renderer.setProjectionMatrix(batch.getProjectionMatrix());
		renderer.setColor(new Color(1,1,1,0.5f));
		renderer.begin(ShapeType.Line);
		float xx = oldPosition.x-position.x;
		xx+=getVelocity()/20;
		float yy = oldPosition.y-position.y;
		yy+=getVelocity()/20;
		renderer.line(position.x, position.y, oldPosition.x+xx, oldPosition.y+yy);
		renderer.end();
		Gdx.gl.glDisable(GL10.GL_BLEND);
//		batch.begin();
		
		
//		if (image ==""){
//			g.setColor(Color.white);
//			double x1 = pos.x+size.x*FastTrig.cos(Math.toRadians(angle));
//			double y1 = pos.y+size.y*FastTrig.sin(Math.toRadians(angle));
//			g.drawLine(pos.x, pos.y, (float)x1,(float) y1);
//		} else {
//			Image img = ResourceManager.getImage(image);
//			img.setRotation((float) angle);
//			img.draw(pos.x, pos.y, size.x, size.y);
//		}
	}

	@Override
	public void createBody() {
		if (body!= null)
			ZombieWorld.world.destroyBody(body);
		body = ZombieWorld.createBulletBox(position.x, position.y, size.x, size.y,angle,density,damping);
		body.setUserData(this);
	}

	public void push(StaticObject obj) {
		float angle = getAngleToObject(obj);
		float x1 = getVelocity()/2*MathUtils.cos(MathUtils.degRad*angle);
		float y1 = getVelocity()/2*MathUtils.sin(MathUtils.degRad*angle);
		obj.body.applyForce(new Vector2((float)x1,(float)y1), body.getWorldCenter());
		obj.life-=damage;
		if (Utils.rnd.nextBoolean())
			obj.getMaterial().getSound().play();
		lifeTime = 0;
	}


	public void setParams(Weapon weapon) {
		damage = weapon.damage;
		density = weapon.bullet.density;
		damping = weapon.bullet.damping;
		setVelocity(weapon.bullet.velocity);
		strongBullet = weapon.strongBullet;
		image = weapon.bullet.image;
		createBody();
	}

	public void doDamage(LiveObject live) {
		Formulas.calcDamage(this,live);
		
		int dmg = damage - live.getHp();		
		if (strongBullet && dmg > 0)
			damage = dmg;
		else
			lifeTime = 0;
	}

}
